package com.geekstudy.core.client;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class RPCAOP {

    final Logger logger = LoggerFactory.getLogger(RPCAOP.class);

    @Pointcut("execution(* com.geekstudy.core.api..*.*())")
    public void point() {

    }

    @Around(value = "point()")
    public Object clientInvoke(ProceedingJoinPoint proceedingJoinPoint) {
        Class<?> targetClass = proceedingJoinPoint.getTarget().getClass();
        String methodName = proceedingJoinPoint.getSignature().getName();
        logger.info(" targetClass:{} methodName:{}", targetClass, methodName);
        Class[] parameterTypeArray = ((MethodSignature)proceedingJoinPoint.getSignature()).getParameterTypes();
        Object[] argArray = proceedingJoinPoint.getArgs();

        Object result = null;
        try {
            // Method method = targetClass.getMethod(methodName, parameterTypeArray);
            result = proceedingJoinPoint.proceed(argArray);

        } catch(Throwable e) {
            logger.error("RPCAOP exception:{}", e );
        }

        return result;
    }





}
